package com.example.project;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;

public class Play_Activity extends Activity implements OnClickListener, OnItemClickListener{
	Button btn_Main_menu, btn_GO;
	TextView lbl_Level;
	GridView gridView;
	int Level; // độ khó
	Bundle extra_values;
	Integer[] Easy_Level = {R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb};
	
	Integer[] Medium_Level = {R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb,
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb};
	
	Integer[] Hard_Level = {R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb, 
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb,
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb,
							 R.drawable.off_bulb, R.drawable.off_bulb, R.drawable.off_bulb};
	
	ArrayList<Integer> bulb_list=new ArrayList<Integer>();
	ArrayList<Integer> ans_list=new ArrayList<Integer>();
	int So_bong_den=1;
	boolean Win=false;
	// Hàm khởi tạo cho các biến Button
	private void Initial_Function()
	{
		btn_Main_menu=(Button)findViewById(R.id.btn_main_menu);
		btn_GO=(Button)findViewById(R.id.btn_go);
		gridView = (GridView)findViewById(R.id.grv_images);
		lbl_Level=(TextView)findViewById(R.id.lbl_level);
		extra_values=getIntent().getExtras();
		if(extra_values!=null)
		{
			Level=extra_values.getInt("Diff_Option");
		}
		bulb_list.clear();
		ans_list.clear();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_play);
		Initial_Function();
		gridView.setAdapter(new Adapter(this));
		gridView.setOnItemClickListener(this);
			
		if(Level==0)
		{
			Danh_sach_bong_den(1, 9);
			lbl_Level.setText(Thu_tu_cac_bong_den(So_bong_den));
		}
		else if(Level==1)
		{
			Danh_sach_bong_den(1, 12);
			lbl_Level.setText(Thu_tu_cac_bong_den(So_bong_den));
		}
		else if(Level==2)
		{
			Danh_sach_bong_den(1, 15);
			lbl_Level.setText(Thu_tu_cac_bong_den(So_bong_den));
		}
		
		btn_Main_menu.setOnClickListener(this);
		btn_GO.setOnClickListener(this);		
	}
	
	// hàm tìm thứ tự bóng đèn
	private int Tim_so_ngau_nhien(int min, int max)
	{
		Random r = new Random();
		int i=r.nextInt(max - min + 1) + min;
		return i;
	}
	
	// kiểm tra tồn tại số trong danh sách bulb_list
	private boolean Ton_tai_trong_danh_sach(int x)
	{
		for(int i=0;i<bulb_list.size();i++)
			if(x==bulb_list.get(i))
				return true;
		return false;
	}
	
	// tạo list thứ tự các bóng đèn
	private void Danh_sach_bong_den(int min, int max)
	{
		int x=Tim_so_ngau_nhien(min, max);
		while (bulb_list.size()<max)
		{
			x=Tim_so_ngau_nhien(min, max);
			if(Ton_tai_trong_danh_sach(x)==false)
				bulb_list.add(x);
		}			
	}
	
	// xuất lên màn hình
	private String Thu_tu_cac_bong_den(int So_bong)
	{
		String thu_tu="";
		for(int i=0;i<So_bong;i++)
			thu_tu=thu_tu+bulb_list.get(i).toString()+", ";
		thu_tu=thu_tu.substring(0, thu_tu.length()-2);
		return thu_tu;
	}
	
	private String Xuat_thu_tu()
	{
		String thu_tu="";
		for(int i=0;i<ans_list.size();i++)
			thu_tu=thu_tu+ans_list.get(i).toString()+", ";
		thu_tu=thu_tu.substring(0, thu_tu.length()-2);
		return thu_tu;
	}
	
	private int Kiem_tra_mo_dung_bong_den()
	{
		if(ans_list.size()>0 && ans_list.size()<So_bong_den)
			return 1;
		for(int i=0;i<ans_list.size();i++)
		{
			if(ans_list.get(i)!=bulb_list.get(i))
			{
				return 1;
			}
		}
		return 2;
	}
	
	
	// hàm làm bóng đèn tắt
	private void Tat_bong_den(View v)
	{
		ImageView Changed_Image = (ImageView) v;
		Changed_Image.setImageResource(R.drawable.off_bulb);
	}
		
	// hàm làm tất cả bóng đèn tắt
	private void Tat_cac_bong_den()
	{
		for(int i=0; i<gridView.getChildCount(); i++)
		{
			Tat_bong_den(gridView.getChildAt(i));
		}
	}
	
	// hàm xử lý 
	private void Ham_xu_ly()
	{
		ans_list.clear();
		lbl_Level.setText(Thu_tu_cac_bong_den(So_bong_den));
		Tat_cac_bong_den();
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_main_menu:	
			if(Win==false)
			{
				AlertDialog dialogBox = makeAndShowDialogBox();
				dialogBox.show();
			}
			else
				finish();
			break;
		case R.id.btn_go:
			if(Win==true)
				break;
			if(ans_list.size()==0)
				break;
			if(Kiem_tra_mo_dung_bong_den() == 1)
			{
				//Toast.makeText(this, Xuat_thu_tu(), Toast.LENGTH_SHORT).show();
				Toast.makeText(this, "Wrong Bulb.", Toast.LENGTH_SHORT).show();
				Ham_xu_ly();
				break;
			}
			So_bong_den=So_bong_den+1;
			if(So_bong_den>bulb_list.size())
			{
				Win=true;
				Toast.makeText(this, "YOU WIN", Toast.LENGTH_SHORT).show();
				break;
			}
			Ham_xu_ly();
			
			break;
		}
	}
	
	// Tạo lớp Adapter
	private class Adapter extends BaseAdapter 
	{
		Context mContext;
		public Adapter(Context mContext){
			this.mContext = mContext;
		}
		// number of images
		@Override
		public int getCount() {
			if(Level==0)
				return Easy_Level.length;
			else if(Level==1)
				return Medium_Level.length;
			return Hard_Level.length;
		}
		// get items
		@Override
		public Object getItem(int position) {
			return null;
		}
		// get items 
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		// method getView
		@Override 
		public View getView(int position, View convertView, ViewGroup parent) { 
			ImageView mImage; 
			if (convertView == null) { 
				mImage = new ImageView(Play_Activity.this); 
				mImage.setLayoutParams(new GridView.LayoutParams(50, 50)); 
				mImage.setScaleType(ScaleType.FIT_XY); 
				convertView = mImage; } 
			else { 
				mImage = (ImageView) convertView; // show image if it has been loaded already
			} 
			if(Level==0)
				mImage.setImageResource(Easy_Level[position]);
			else if(Level==1)
				mImage.setImageResource(Medium_Level[position]); 
			if(Level==2)
				mImage.setImageResource(Hard_Level[position]); 
			return mImage; 
		} 
	}// Hết lớp Adapter
	
	@Override
	public void onItemClick( AdapterView<?> parent, View v, int position, long id) {
		lbl_Level.setText("");
		ImageView Changed_Image = (ImageView) v;
		Changed_Image.setImageResource(R.drawable.on_bulb);
		int i=position+1;
		ans_list.add(i);
	}//onItemClick
	
	// Tạo AlertDialog
	private AlertDialog makeAndShowDialogBox(){
		AlertDialog myQuittingDialogBox =
		new AlertDialog.Builder(this)
		// thiết lập tiêu đề và thông điệp
		.setTitle("Termination")
		.setMessage("If you quit the game, you'll lose it. Do you still want to do this?")		
			
		//xử lý khi người chơi chọn YES
		.setPositiveButton("YES", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				finish();
				return;
			}
		})
			// xử lý khi người chơi chọn NO
		.setNegativeButton("NO", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				return;
			}
		})
			
		.create();
		return myQuittingDialogBox;
	}
}
